<template>
  <div class="app-container">
    <el-card shadow="never" class="filter-bar" >
      <div class="btn-group pull-left">
        <el-button @click="HandleNew" type="success" icon="el-icon-edit" size="mini">添加</el-button>
        <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
      </div>

      <el-form :inline="true" :model="form" class="pull-right" size="mini">
        <el-form-item label="">
          <el-input v-model="form.keyword" placeholder="店铺名称或者号码"></el-input>
        </el-form-item>
        <el-form-item label="审核状态">
          <el-select v-model="form.check" placeholder="选择审核状态">
            <el-option label="全部" value=""></el-option>
            <el-option label="待审核" value="0"></el-option>
            <el-option label="通过审核" value="1"></el-option>
            <el-option label="未通过审核" value="-1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card shadow="never" class="store-table" >
      <el-table
        v-loading="loading"
        :data="list"
        style="width: 100%">
        <el-table-column
          prop="name"
          label="货品名称">
        </el-table-column>

        <el-table-column
          label="价格"
          width="180">
          <template slot-scope="scope">
            <span>{{ scope.row.price }}</span>
          </template>
        </el-table-column>

        <el-table-column
          label="所属店铺"
          width="200">
          <template slot-scope="scope">
            <span>{{ scope.row.store.name }}</span>
          </template>
        </el-table-column>

        <el-table-column
          label="货品状态"
          width="180">
          <template slot-scope="scope">
            <el-tag
            :type="[scope.row.status, 'class'] | statusFilter"
            close-transition>{{[scope.row.status, 'text'] | statusFilter}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="created_at"
          label="创建日期"
          width="180">
        </el-table-column>
        <el-table-column
          label="操作"
          width="150">
        <template slot-scope="scope">
          <router-link :to="{name:'products-show', params: {id: scope.row.id}}">
            <el-button type="text" size="small">查看</el-button>
          </router-link>
          
          <el-button type="text" size="small">下架</el-button>

          <router-link :to="{name:'products-edit', params: {id: scope.row.id}}">
            <el-button type="text" size="small">编辑</el-button>
          </router-link>
          
        </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageMeta.page"
          :page-sizes="[15, 25, 50, 100]"
          :page-size="pageMeta.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageMeta.total">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { fetchList } from '@/api/product'

const statusMap = {
  '0': {
    'text': '已下架',
    'class': 'danger'
  },
  '1': {
    'text': '已上架',
    'class': 'success'
  }
}
export default {
  name: 'store',
  data() {
    return {
      list: [],
      pageMeta: {
        total: 0,
        size: 15,
        page: 1
      },
      form: {
        keyword: '',
        check: ''
      },
      loading: false
    }
  },
  created() {
    this.getStoreslist()
  },
  activate(transition) {
    this.getStoreslist()
    this.pre_params = this.$route.query
    if (this.pre_params.user_id) {
      // do something
      // ajax等在渲染组件前的逻辑操作
    }
    transition.next()
  },
  methods: {
    getStoreslist() {
      this.loading = true
      fetchList({
        page: this.pageMeta.page,
        size: this.pageMeta.size,
        keyword: this.form.keyword,
        check: this.form.check
      }).then(res => {
        const data = res.data
        this.list = data.data
        this.pageMeta.total = data.total
        this.loading = false
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    // 重设每页显示数量
    handleSizeChange(val) {
      this.pageMeta.size = val
      this.getStoreslist()
    },
    // 翻页
    handleCurrentChange(val) {
      this.pageMeta.page = val
      this.getStoreslist()
    },
    onSubmit() {
      this.pageMeta.page = 1
      this.pageMeta.size = 15
      this.getStoreslist()
    },
    // 添加新的
    HandleNew() {
      this.$router.push({ name: 'products-creat' })
    }
  },
  filters: {
    statusFilter: function([status, type]) {
      return statusMap[status][type]
    }
  }
}
</script>

<style>
.store-table .store-table-expand label {
    width: 90px;
    color: #76838f;
    font-weight: normal;
  }
</style>

<style lang="scss" scoped>
.store-table .store-table-expand {
  .el-form-item {
      margin-right: 0;
      margin-bottom: 0;
      width: 40%;
  }
}
</style>
